﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jQueryDotNetLib.Controls;


namespace jQueryDotNetLib.Extenders
{
    [ToolboxItem(true)]
    [ToolboxData("<{0}:Sortable Selector=\"\" runat=server />")]
    public class Sortable : BaseInteractionExtender
    {
        public Sortable()
        {
            Interaction = "sortable";
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientStart
        {
            get
            {
                if (ViewState["OnClientStart"] == null) return "";
                return (string)ViewState["OnClientStart"];
            }
            set
            {
                ViewState["OnClientSelected"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientSort
        {
            get
            {
                if (ViewState["OnClientSort"] == null) return "";
                return (string)ViewState["OnClientSort"];
            }
            set
            {
                ViewState["OnClientSort"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientChange
        {
            get
            {
                if (ViewState["OnClientChange"] == null) return "";
                return (string)ViewState["OnClientChange"];
            }
            set
            {
                ViewState["OnClientChange"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientBeforeStop
        {
            get
            {
                if (ViewState["OnClientBeforeStop"] == null) return "";
                return (string)ViewState["OnClientBeforeStop"];
            }
            set
            {
                ViewState["OnClientBeforeStop"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientStop
        {
            get
            {
                if (ViewState["OnClientStop"] == null) return "";
                return (string)ViewState["OnClientStop"];
            }
            set
            {
                ViewState["OnClientStop"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientUpdate
        {
            get
            {
                if (ViewState["OnClientUpdate"] == null) return "";
                return (string)ViewState["OnClientUpdate"];
            }
            set
            {
                ViewState["OnClientUpdate"] = value;
            }
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientReceive
        {
            get
            {
                if (ViewState["OnClientReceive"] == null) return "";
                return (string)ViewState["OnClientReceive"];
            }
            set
            {
                ViewState["OnClientReceive"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientRemove
        {
            get
            {
                if (ViewState["OnClientRemove"] == null) return "";
                return (string)ViewState["OnClientRemove"];
            }
            set
            {
                ViewState["OnClientRemove"] = value;
            }
        }
        

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientOver
        {
            get
            {
                if (ViewState["OnClientOver"] == null) return "";
                return (string)ViewState["OnClientOver"];
            }
            set
            {
                ViewState["OnClientOver"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientOut
        {
            get
            {
                if (ViewState["OnClientOut"] == null) return "";
                return (string)ViewState["OnClientOut"];
            }
            set
            {
                ViewState["OnClientOut"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientActivate
        {
            get
            {
                if (ViewState["OnClientActivate"] == null) return "";
                return (string)ViewState["OnClientActivate"];
            }
            set
            {
                ViewState["OnClientActivate"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientDeactivate
        {
            get
            {
                if (ViewState["OnClientDeactivate"] == null) return "";
                return (string)ViewState["OnClientDeactivate"];
            }
            set
            {
                ViewState["OnClientDeactivate"] = value;
            }
        }



        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ClientStyle
        {
            get
            {
                if (ViewState["ClientStyle"] == null) return "";
                return (string)ViewState["ClientStyle"];
            }
            set
            {
                ViewState["ClientStyle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ClientListItemStyle
        {
            get
            {
                if (ViewState["ClientListItemStyle"] == null) return "";
                return (string)ViewState["ClientListItemStyle"];
            }
            set
            {
                ViewState["ClientListItemStyle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ClientListItemSpanStyle
        {
            get
            {
                if (ViewState["ClientListItemSpanStyle"] == null) return "";
                return (string)ViewState["ClientListItemSpanStyle"];
            }
            set
            {
                ViewState["ClientListItemSpanStyle"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ClientListItemHighlightStyle
        {
            get
            {
                if (ViewState["ClientListItemHighlightStyle"] == null) return "";
                return (string)ViewState["ClientListItemHighlightStyle"];
            }
            set
            {
                ViewState["ClientListItemHighlightStyle"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AppendTo
        {
            get
            {
                if (ViewState["AppendTo"] == null) return "";
                return (string)ViewState["AppendTo"];
            }

            set
            {
                ViewState["AppendTo"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public AxisOptions Axis
        {
            get
            {
                if (ViewState["Axis"] == null) return AxisOptions.none;
                return (AxisOptions)ViewState["Axis"];
            }

            set
            {
                ViewState["Axis"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Cancel
        {
            get
            {
                if (ViewState["Cancel"] == null) return "";
                return (string)ViewState["Cancel"];
            }

            set
            {
                ViewState["Cancel"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ConnectWith
        {
            get
            {
                if (ViewState["ConnectWith"] == null) return "";
                return (string)ViewState["ConnectWith"];
            }

            set
            {
                ViewState["ConnectWith"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Containment
        {
            get
            {
                if (ViewState["Containment"] == null) return "";
                return (string)ViewState["Containment"];
            }

            set
            {
                ViewState["Containment"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Cursor
        {
            get
            {
                if (ViewState["Cursor"] == null) return "";
                return (string)ViewState["Cursor"];
            }

            set
            {
                ViewState["Cursor"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CursorAt
        {
            get
            {
                if (ViewState["CursorAt"] == null) return "";
                return (string)ViewState["CursorAt"];
            }

            set
            {
                ViewState["CursorAt"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Delay
        {
            get
            {
                if (ViewState["Delay"] == null) return 0;
                return (int)ViewState["Delay"];
            }

            set
            {
                ViewState["Delay"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Distance
        {
            get
            {
                if (ViewState["Distance"] == null) return 1;
                return (int)ViewState["Distance"];
            }

            set
            {
                ViewState["Distance"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool DropOnEmpty
        {
            get
            {
                if (ViewState["DropOnEmpty"] == null) return true;
                return (bool)ViewState["DropOnEmpty"];
            }

            set
            {
                ViewState["DropOnEmpty"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ForcePlaceholderSize
        {
            get
            {
                if (ViewState["ForcePlaceholderSize"] == null) return false;
                return (bool)ViewState["ForcePlaceholderSize"];
            }

            set
            {
                ViewState["ForcePlaceholderSize"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ForceHelperSize
        {
            get
            {
                if (ViewState["ForceHelperSize"] == null) return false;
                return (bool)ViewState["ForceHelperSize"];
            }

            set
            {
                ViewState["ForceHelperSize"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Grid
        {
            get
            {
                if (ViewState["Grid"] == null) return "";
                return (string)ViewState["Grid"];
            }

            set
            {
                ViewState["Grid"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Handle
        {
            get
            {
                if (ViewState["Handle"] == null) return "";
                return (string)ViewState["Handle"];
            }

            set
            {
                ViewState["Handle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Helper
        {
            get
            {
                if (ViewState["Helper"] == null) return "";
                return (string)ViewState["Helper"];
            }

            set
            {
                ViewState["Helper"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Items
        {
            get
            {
                if (ViewState["Items"] == null) return "";
                return (string)ViewState["Items"];
            }

            set
            {
                ViewState["Items"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public float Opacity
        {
            get
            {
                if (ViewState["Opacity"] == null) return 0;
                return (float)ViewState["Opacity"];
            }

            set
            {
                ViewState["Opacity"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Placeholder
        {
            get
            {
                if (ViewState["Placeholder"] == null) return "";
                return (string)ViewState["Placeholder"];
            }

            set
            {
                ViewState["Placeholder"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public RevertOptions Revert
        {
            get
            {
                if (ViewState["Revert"] == null) return RevertOptions.no;
                return (RevertOptions)ViewState["Revert"];
            }

            set
            {
                ViewState["Revert"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Scroll
        {
            get
            {
                if (ViewState["Scroll"] == null) return true;
                return (bool)ViewState["Scroll"];
            }

            set
            {
                ViewState["Scroll"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ScrollSensitivity
        {
            get
            {
                if (ViewState["ScrollSensitivity"] == null) return 20;
                return (int)ViewState["ScrollSensitivity"];
            }

            set
            {
                ViewState["ScrollSensitivity"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ScrollSpeed
        {
            get
            {
                if (ViewState["ScrollSpeed"] == null) return 20;
                return (int)ViewState["ScrollSpeed"];
            }

            set
            {
                ViewState["ScrollSpeed"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public ToleranceOptions Tolerance
        {
            get
            {
                if (ViewState["Tolerance"] == null) return ToleranceOptions.intersect;
                return (ToleranceOptions)ViewState["Tolerance"];
            }

            set
            {
                ViewState["Tolerance"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ZIndex
        {
            get
            {
                if (ViewState["ZIndex"] == null) return 1000;
                return (int)ViewState["ZIndex"];
            }

            set
            {
                ViewState["ZIndex"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {


            AddBinding("sortstart", OnClientStart);
            AddBinding("sort", OnClientSort);
            AddBinding("sortchange", OnClientChange);
            AddBinding("sortbeforeStop", OnClientBeforeStop);
            AddBinding("sortstop", OnClientStop);
            AddBinding("sortupdate", OnClientUpdate);
            AddBinding("sortreceive", OnClientReceive);
            AddBinding("sortremove", OnClientRemove);
            AddBinding("sortover", OnClientOver);
            AddBinding("sortout", OnClientOut);
            AddBinding("sortactivate", OnClientActivate);
            AddBinding("sortdeactivate", OnClientDeactivate);

            if (AppendTo != "") AddParameter("appendTo", "'" + AppendTo + "'");
            if (Axis != AxisOptions.none) AddParameter("axis", "'" + Axis.ToString() + "'");
            if (Cancel != "") AddParameter("cancel", "'" + Cancel + "'");
            if (ConnectWith != "") AddParameter("connectWith", "'" + ConnectWith + "'");
            if (Containment != "") AddParameter("containment", "'" + Containment + "'");
            if (Cursor != "") AddParameter("cursor", "'" + Cursor + "'");
            if (CursorAt != "") AddParameter("cursorAt", "{" + CursorAt + "}");
            if (Delay != 0) AddParameter("delay", "" + Delay + "");
            if (Distance != 1) AddParameter("distance", "" + Distance + "");
            if (!DropOnEmpty) AddParameter("dropOnEmpty", "false");
            if (ForceHelperSize) AddParameter("forceHelperSize", "true");
            if (ForcePlaceholderSize) AddParameter("forcePlaceholderSize", "true");
            if (Grid != "") AddParameter("grid", "[" + Grid + "]");
            if (Handle != "") AddParameter("handle", "'" + Handle + "'");
            if (Helper != "") AddParameter("helper", "'" + Helper + "'");
            if (Items!= "") AddParameter("items", "'" + Items + "'");
            if (Opacity != 0) AddParameter("opacity", "'" + Opacity + "'");
            if (Placeholder != "" && Placeholder != "false") AddParameter("placeHolder", "'" + Placeholder + "'");
            if (Revert != RevertOptions.no)
            {
                if (Revert == RevertOptions.no)
                    AddParameter("revert", "false");
                else if (Revert == RevertOptions.yes)
                    AddParameter("revert", "true");
                else
                    AddParameter("revert", "'" + Revert + "'");
            }
            if (!Scroll) AddParameter("scroll", "false");
            if (ScrollSensitivity != 20) AddParameter("scrollSensitivity", "'" + ScrollSensitivity + "'");
            if (ScrollSpeed != 20) AddParameter("scrollSpeed", "'" + ScrollSpeed + "'");
            if (Tolerance != ToleranceOptions.intersect) AddParameter("tolerance", Tolerance.ToString());

            if (ZIndex != 1000)
                AddParameter("zIndex", ZIndex.ToString() );


            writer.Write("<STYLE type=text/css>");

            if (ClientListItemHighlightStyle != "") writer.Write(".ui-state-highlight { " + ClientListItemHighlightStyle + " }");
            if (ClientStyle != "") writer.Write(ResolvedSelector+ " {" + ClientStyle + " }");
            if (ClientListItemStyle != "") writer.Write(ResolvedSelector + " li { " + ClientListItemStyle + " }");
            if (ClientListItemSpanStyle != "") writer.Write(ResolvedSelector + " li span { " + ClientListItemSpanStyle + " }");
            writer.Write("</STYLE>");

            base.Render(writer);
        }
    }
}
